package com.lineage.server.serverpackets;

import com.lineage.server.datatables.ItemSpecialAttributeTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.templates.L1ItemSpecialAttribute;
import com.lineage.server.templates.L1ItemSpecialAttributeChar;

public class S_NPCPack_Item extends ServerBasePacket {
  private byte[] _byte;
  
  public S_NPCPack_Item(L1ItemInstance item) {
    this._byte = null;
    buildPacket(item);
  }
  
  private void buildPacket(L1ItemInstance item) {
    writeC(87);
    writeH(item.getX());
    writeH(item.getY());
    writeD(item.getId());
    writeH(item.getItem().getGroundGfxId());
    writeC(0);
    writeC(0);
    writeC(item.isNowLighting() ? item.getItem().getLightRange() : 0);
    writeC(0);
    writeD((int)Math.min(item.getCount(), 2000000000L));
    writeH(0);
    String name = "";
    if (item.getCount() > 1L) {
      name = String.valueOf(String.valueOf(item.getItem().getNameId())) + " (" + item.getCount() + ")";
    } else {
      switch (item.getItemId()) {
        case 20383:
        case 41235:
        case 41236:
          name = String.valueOf(String.valueOf(item.getItem().getNameId())) + " [" + item.getChargeCount() + "]";
          break;
        case 40006:
        case 40007:
        case 40008:
        case 40009:
        case 140006:
        case 140008:
          if (item.isIdentified())
            name = String.valueOf(String.valueOf(item.getItem().getNameId())) + " (" + item.getChargeCount() + ")"; 
          break;
        default:
          if (item.getItem().getLightRange() != 0 && item.isNowLighting()) {
            name = String.valueOf(String.valueOf(item.getItem().getNameId())) + " ($10)";
            break;
          } 
          name = item.getItem().getNameId();
          break;
      } 
    } 
    L1ItemSpecialAttributeChar attr_char = item.get_ItemAttrName();
    if (attr_char != null) {
      L1ItemSpecialAttribute attr = ItemSpecialAttributeTable.get().getAttrId(attr_char.get_attr_id());
      writeS(String.valueOf(String.valueOf(attr.get_colour())) + attr.get_name() + name);
    } else {
      writeS(name);
    } 
    writeS(null);
    writeC(0);
    writeD(0);
    writeS(null);
    writeS(null);
    writeC(0);
    writeC(255);
    writeC(0);
    writeC(0);
    writeC(0);
    writeC(255);
    writeC(255);
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
